using System.Collections.Generic;
using System.Linq;

namespace PodcastGenerator
{
    public abstract class Store : IStore
    {
        protected ISermonDownloader SermonDownloader;

        public int PersistAll(List<ISermon> sermons)
        {
            foreach (var sermon in sermons)
            {
                SermonDownloader.Download(sermon);
            }

            PersistToDataStore(sermons, false);
            return sermons.Count;
        }

        public List<ISermon> PersistNew(List<ISermon> sermons)
        {
            var sermonsToPersist = sermons.Where(sermon => !SermonDownloader.ExistInStoreLocation(sermon)).ToList();
            foreach (var sermon in sermonsToPersist)
            {
                SermonDownloader.Download(sermon);
            }

            PersistToDataStore(sermonsToPersist, true);
            return sermonsToPersist;
        }

        protected abstract void PersistToDataStore(IEnumerable<ISermon> sermons, bool append);
    }
}